﻿using System;
using System.Collections.Generic;
using VMuktiService;

namespace VMuktiAPI
{
    public static partial class VMuktiHelper
    {
        public static List<VMuktiEvents> VMEvents = new List<VMuktiEvents>();

        public static List<NetPeerServer> VMuktiServers = new List<NetPeerServer>();

        public static void CallEvent(string eventName, object sender, VMuktiEventArgs e)
        {
            foreach (VMuktiEvents ve in VMEvents)
            {
                if (ve.EventName.Equals(eventName))
                {
                    ve.FireVMuktiEvent(sender, e);
                    break;
                }
            }
        }

        public static VMuktiEvents RegisterEvent(string eventName)
        {
            //VMEvents.Add(new VMuktiEvents(eventName));
            //return VMEvents[VMEvents.Count - 1];

            bool isEventExists = false;
            int i = 0;
            // foreach (VMuktiEvents ve in VMEvents)
            for (i = 0; i < VMEvents.Count; i++)
            {
                if (VMEvents[i].EventName.Equals(eventName))
                {
                    isEventExists = true;
                    break;
                }
            }
            if (!isEventExists)
            {
                VMEvents.Add(new VMuktiEvents(eventName));
                return VMEvents[VMEvents.Count - 1];
            }
            else
            {
                return VMEvents[i];
            }

        }

        public static void UnRegisterEvent(string eventName)
        {
            for(int i = 0 ;i<VMEvents.Count;i++)
            {
                if (VMEvents[i].EventName == eventName)
                {
                    VMEvents.RemoveAt(i);
                    break;
                }
            }

        }

        public static string[] StartAServer()
        {
            try
            {
                VMuktiServers.Add(new NetPeerServer("net.tcp://" + Environment.MachineName + ":2500/" + VMuktiServers.Count.ToString()));
                VMuktiServers[VMuktiServers.Count - 1].AddEndPoint("net.tcp://" + Environment.MachineName + ":2500/" + Convert.ToString(VMuktiServers.Count - 1));
                VMuktiServers[VMuktiServers.Count - 1].Name = "net.tcp://" + Environment.MachineName + ":2500/" + Convert.ToString(VMuktiServers.Count - 1);
                VMuktiServers[VMuktiServers.Count - 1].OpenServer();

                return new string[] { "net.tcp://" + Environment.MachineName + ":2500/" + Convert.ToString(VMuktiServers.Count - 1), VMuktiAPI.VMuktiInfo.CurrentPeer.MeshID + Convert.ToString(VMuktiServers.Count - 1) };
            }
            catch
            {
                return null;
            }
        }

        public static void StopAServer(string URI)
        {
            for (int i = 0; i < VMuktiServers.Count; i++)
            {
                if (VMuktiServers[i].Name == URI)
                {
                    VMuktiServers[i].CloseServer();
                    break;
                }
            }
        }
    }

    #region Add Events
    public class VMuktiEvents
    {
        public string EventName = "";
        public delegate void VMuktiEventHandler(object sender, VMuktiEventArgs e);
        public event VMuktiEventHandler VMuktiEvent;

        public VMuktiEvents(string eventName)
        {
            EventName = eventName;
        }

        public void FireVMuktiEvent(object sender, VMuktiEventArgs e)
        {
            if (VMuktiEvent != null)
            {
                VMuktiEvent(sender, e);
            }
        }


    }
    #endregion

    # region VMukti Event Args
    public class VMuktiEventArgs : EventArgs
    {
        public List<object> _args = new List<object>();
        public VMuktiEventArgs(params object[] parameters)
        {
            _args.AddRange(parameters);
            
        }
    }
    #endregion

    public class ModuleServer
    {
        //ModuleServer(string UniqueStr)
        //{
        //}
    }
}